package com.shtel.paas.monitorcenter.process.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.shtel.paas.monitorcenter.process.management.entity.AlarmFrequencyEntity;
import com.shtel.paas.monitorcenter.process.management.entity.HostInformation;
import com.shtel.paas.monitorcenter.process.management.entity.ProcessInfoParam;
import com.shtel.paas.monitorcenter.process.management.entity.ProcessInforEntity;
import com.shtel.paas.monitorcenter.process.management.mappper.ProcessListMapper;
import com.shtel.paas.monitorcenter.process.management.service.ProcessInfoDataService;
import com.shtel.paas.monitorcenter.util.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName
 * @Description Too
 * @Author ME
 * @Data 11:04
 **/
@Service
public class ProcessInfoDataServiceImpl implements ProcessInfoDataService {

    @Autowired
    private ProcessListMapper processListMapper;




    //查询列表分页
    public Page<ProcessInforEntity> selectAllProccessInfo(int page, int size){
        PageHelper.startPage(page, size);
        List<ProcessInforEntity> processInforEntityList = new ArrayList<ProcessInforEntity>();
        try {
            processInforEntityList = new ArrayList<ProcessInforEntity>();
            processInforEntityList = processListMapper.selectAll();

        } catch (Exception e) {
            e.printStackTrace();
        }
        Page<ProcessInforEntity> pageContent = Page.build(processInforEntityList);
        return pageContent;
    }


    @Override
    public List<ProcessInforEntity> selectAllProccessInfo() {

        List<ProcessInforEntity> processInforEntityList = new ArrayList<ProcessInforEntity>();

        try {
            processInforEntityList = new ArrayList<ProcessInforEntity>();
            processInforEntityList = processListMapper.selectAll();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return processInforEntityList;
    }

    @Override
    public int deleteProcessInfo(ProcessInforEntity param){
        int dao=0;

        try {

               //当AlarmFrequencyId！=1时，需删除AlarmFrequency表中华数据
               List<ProcessInforEntity> processInforEntities = processListMapper.selectSingleInfo("", param.getId());
                if(processInforEntities.size()>0 &&  processInforEntities.get(0).getAlarmFrequencyId()!=1){
                    int i = processListMapper.deleteAlarmFrequency(processInforEntities.get(0).getAlarmFrequencyId());
                   // System.out.println("-------deleteProcessInfo  AlarmFrequencyId  -------"+processInforEntities.get(0).getAlarmFrequencyId());
                    if(i==0){
                        System.out.println("----XXXX删除出现异常-----------"+param.getId());
                        return 0;
                    }
                }

                    try {
                       // System.out.println("--serviceimpl---------------" + param.getHostid());
                        dao = processListMapper.deleteProcessInfo(param.getId());
                      //  System.out.println("删除条数：" + dao);
                } catch (Exception e) {
                    System.out.println("XXXX删除出现异常：" + e.getMessage());
                }


        } catch (Exception e) {
            e.printStackTrace();
        }


        return dao;
    }

    @Override
    public List<HostInformation> selectHostInfo(int hostid){
        try {
            System.out.println("ProcessInfoDataServiceImpl_selectHostInfo: "+hostid);
            List<HostInformation> hostInformation = null;
            try {
                hostInformation = processListMapper.selectHostInfo(hostid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if(hostInformation.size()>=1 && hostInformation!=null ){
                return hostInformation;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertProcessInfo(ProcessInforEntity paramList,AlarmFrequencyEntity paramFrequ) {
        int insetProcessInfo = 0;
        try {
                    if(paramFrequ.getMinutes()==0 && paramFrequ.getTimes()==0){
                        insetProcessInfo = processListMapper.insetProcessInfo(paramList);

                    }else{

                        int i = processListMapper.insertAlarmFrequency(paramFrequ);
                        //若AlarmFrequency 插入数据失败，return 0
                        if(i==0){
                            return 0;
                        }else{
                            AlarmFrequencyEntity alarmFrequencyEntity = processListMapper.selectMaxFrequency(paramFrequ.getTimes(), paramFrequ.getMinutes());
                            paramList.setAlarmFrequencyId(alarmFrequencyEntity.getId());
                            insetProcessInfo = processListMapper.insetProcessInfo(paramList);
                        }
                    }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return insetProcessInfo;
    }

    @Override
    public int updateProcessInfo(ProcessInforEntity param,AlarmFrequencyEntity paramFrequ) {

        int updateProcessInfo = 0;
        try {
            if(paramFrequ.getMinutes()==0 && paramFrequ.getTimes()==0){//取默认值
                List<ProcessInforEntity> processInforEntities = processListMapper.selectSingleInfo(param.getIp(), param.getId());
                if (processInforEntities.get(0).getAlarmFrequencyId() == 1) {
                    updateProcessInfo = processListMapper.updateProcessInfo(param);
                }else{
                    int i = processListMapper.deleteAlarmFrequency(processInforEntities.get(0).getAlarmFrequencyId());
                    if (i == 0) {
                        System.out.println("============" + processInforEntities.get(0).getId() + "//" + processInforEntities.get(0).getAlarmFrequencyId() + " 删除失败============");
                        return 0;
                    }else{
                        updateProcessInfo = processListMapper.updateProcessInfo(param);
                    }
                }

            }else{
                int i = processListMapper.insertAlarmFrequency(paramFrequ);
                //若AlarmFrequency 插入数据失败，return 0
                if(i==0){
                    return 0;
                }
                  //若之前规则不是默认，删除
                   List<ProcessInforEntity> processInforEntities = processListMapper.selectSingleInfo(param.getIp(), param.getId());
                    if(processInforEntities.get(0).getAlarmFrequencyId()!=1){
                        int j = processListMapper.deleteAlarmFrequency(processInforEntities.get(0).getAlarmFrequencyId());
                        if(j==0){
                            return 0;
                        }
                    }

                    AlarmFrequencyEntity alarmFrequencyEntity = processListMapper.selectMaxFrequency(paramFrequ.getTimes(), paramFrequ.getMinutes());
                    param.setAlarmFrequencyId(alarmFrequencyEntity.getId());
                    updateProcessInfo = processListMapper.updateProcessInfo(param);

            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return updateProcessInfo;
    }

    @Override
    public List<ProcessInforEntity> selectSingleInfo(String ip) {
        try {
            System.out.println("==================="+ip+"====================");
            List<ProcessInforEntity> processInfor= processListMapper.selectSingleInfo(ip,0);
            if(processInfor.size()>=1 &&processInfor !=null){
                return processInfor;
            }
        } catch (SQLException e) {
            System.out.println("----------查询失败：ip+“”+------------");
        }
        return null;
    }



}
